import {Coolcar} from "./request";

export namespace TripService {
    export function CreateTrip(req: any): Promise<any> {
        return Coolcar.sendRequestWithAuthRetry({
            method: "POST",
            path: '/v1/trip',
            data: req
        })
    }

    export function GetTrip(id: string): Promise<any> {
        return Coolcar.sendRequestWithAuthRetry({
            method: "GET",
            path: `/v1/trip/${encodeURIComponent(id)}`,
        })
    }

    export function GetTrips(s ?: number): Promise<any> {
        let path = `/v1/trips`
        if (s) {
            path += "?status=" + s
        }

        return Coolcar.sendRequestWithAuthRetry({
            method: "GET",
            path,
        })
    }

    export function updateTripPos(id: string, loc?:any) {
        return updateTrip({
            id,
            current: loc,
        })
    }

    export function finishTrip(id: string) {
        return updateTrip({
            id,
            endTrip: true,
        })
    }

    function updateTrip(r:any): Promise<any> {
        if (!r.id) {
            return Promise.reject("must specify id")
        }
        return Coolcar.sendRequestWithAuthRetry({
            method: 'PUT',
            path: `/v1/trip/${encodeURIComponent(r.id)}`,
            data: r,
        })
    }
}